﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ESF.Web.Pages;
using ESF.General;
using ESF.SSO.Presenter.Admin.User;
using ESF.General.Entity;
using ESF.SSO.Core.Entities;
using ESF.Web;
using ESF.Web.ServerControls;
using ESF.SSO.Core.Entities.BasicReferences;
using ESF.SSO.Web.Tools.Modules;

#endregion


public partial class User_BrowseUsers : BaseCustomBrowsePagedPage<User, BrowseUsersPresenter, IBrowseUsersViewer>, IBrowseUsersViewer
{
	#region Propiedades

	public List<BasicReference> UserGroupList
	{
		set
		{
			ListLoader.LoadList (ddlUserGroup, value);
		}
	}


	public BasicReference UserGroup
	{
		set
		{
			ListLoader.Select (ddlUserGroup, value);
		}
		get
		{
			return ListLoader.GetSelectedBasicReference (ddlUserGroup);
		}
	}


	public List<BasicReference> ExtraAppSteps
	{
		get
		{
			return ViewState["ExtraAppSteps"] as List<BasicReference>;
		}
		set
		{
			ViewState["ExtraAppSteps"] = value;
		}
	}


	public string filExtraValue
	{
		set
		{
			JS.AddEndJS(Page, "ClearExtraValue ();");
			hidExtraValue.Value = string.Empty;
		}
		get
		{
			return hidExtraValue.Value;
		}
	}

	#endregion


	#region Carga Inicial
	
	protected void Page_Load(object sender, EventArgs e)
	{
		_Presenter = new BrowseUsersPresenter (this);
	
		if (!IsPostBack)
		{
			lblTitle.Text = Title;
			
			ListLoader.LoadList (ddlIdentificationType, WebGeneral.ListBasicReference (BasicReferenceTypes.IdentificationType));

			_Presenter.Load();


			#region Extra Pasos

			ifrStep_3.Visible = false;

			if (!DataHelper.IsEmpty(ExtraAppSteps))
			{
				ifrStep_3.Visible = true; 
				
				btnBrowse.OnClientClick = "SetExtraValue ();";

				JS.AddEndJS(Page, "function ClearExtraValue () { var ifr = document.getElementById ('" + ifrStep_3.ClientID + "'); ifr.contentWindow.SetValue (''); }");

				JS.AddEndJS(Page, "function SetExtraValue () { var ifr = document.getElementById ('" + ifrStep_3.ClientID + "'); $('#" + hidExtraValue.ClientID + "').val (ifr.contentWindow.GetValue ()); }");

				JS.AddEndJS(Page, "ifrStep_3_IsValid = function (source, args) { var ifr = document.getElementById ('" + ifrStep_3.ClientID + "'); args.IsValid = ifr.contentWindow.IsValid (); }");

				cvlIframeOtherApps.ClientValidationFunction = "ifrStep_3_IsValid";

				// TODO: soportar mas de una aplicacion
				foreach (BasicReference app in ExtraAppSteps)
				{
					ifrStep_3.Attributes["src"] = RequestHelper.AppendVarValueToHTTPURL(app.Extra_I + "/" + app.Extra_II, PageHelper.KEY_SETDOCUMENTDOMAIN, AuthenticationModule.TicketDomain);
				}
			}
			else
			{
				cvlIframeOtherApps.Enabled = false;
			}

			#endregion
			
			btnNew.Attributes ["onclick"] = JS.GenerateGoToFinal ("EditUser.aspx?new=1");
		}
	}
	
	#endregion
	
	
	#region Funciones Sobreescritas

	protected override Button GetDeleteButton()
	{
		return btnDelete;
	}


	protected override DataPagging GetDataPagging()
	{
		return dpggrvResults;
	}


	protected override GridView GetGridView()
	{
		return grvResults;
	}


	public override IEnumerable<User> GetSelectedItems(IEnumerable<string> pIDs)
	{
		foreach (string id in pIDs)
		{
			yield return new User () { ID = TextHelper.GetInt (id) };
		}
	}

	#endregion


	#region IBrowseUsersViewer Members

	public string UserAlias
	{
		get
		{
			return txtAlias.Text;
		}
		set
		{
			txtAlias.Text = value;
		}
	}

	public string UserEmail
	{
		get
		{
			return txtEmail.Text;
		}
		set
		{
			txtEmail.Text = value;
		}
	}

	public bool? IsEnabled
	{
		get
		{
			return TextHelper.GetNullableBool (ddlIsEnabled.SelectedValue);
		}
		set
		{
			if (value == null) { ddlIsEnabled.SelectedValue = string.Empty; }
			else { ddlIsEnabled.SelectedValue = value.Value.ToString (); }
		}
	}

	public string UserFirstName
	{
		get
		{
			return txtFirstName.Text;
		}
		set
		{
			txtFirstName.Text = value;
		}
	}

	public string UserSecondName
	{
		get
		{
			return txtSecondName.Text;
		}
		set
		{
			txtSecondName.Text = value;
		}
	}

	public string UserLastName
	{
		get
		{
			return txtLastName.Text;
		}
		set
		{
			txtLastName.Text = value;
		}
	}

	public string UserSurName
	{
		get
		{
			return txtSurName.Text;
		}
		set
		{
			txtSurName.Text = value;
		}
	}

	public int? UserIdentificationTypeID
	{
		get
		{
			return TextHelper.GetNullableInt (ddlIdentificationType.SelectedValue);
		}
		set
		{
			ListLoader.Select <DropDownList> (ddlIdentificationType, value);
		}
	}

	public string UserIdentificationNumber
	{
		get
		{
			return txtIdentificationNumber.Text;
		}
		set
		{
			txtIdentificationNumber.Text = value;
		}
	}
	
	
	public List<BasicReference> EnabledList
	{
		set
		{
			ListLoader.LoadList<DropDownList, BasicReference> (ddlIsEnabled, value, "Code", "Name");
		}
	}


	public string UserEnterpriseName
	{
		set
		{
			txtUserEnterprise.Text = value;
		}
		get
		{
			return txtUserEnterprise.Text;
		}
	}


	public string UserProfessionName
	{
		set
		{
			txtUserProfession.Text = value;
		}
		get
		{
			return txtUserProfession.Text;
		}
	}


	public string UserPositionName
	{
		set
		{
			txtUserPosition.Text = value;
		}
		get
		{
			return txtUserPosition.Text;
		}
	}

	#endregion


	#region Eventos de la Grilla
	
	protected void grvResults_RowDataBound(object sender, GridViewRowEventArgs e)
	{
		User item;
		
	
		if (e.Row.RowType == DataControlRowType.DataRow)
		{
			item = e.Row.DataItem as User;
		
			GridLoader.SetValueIDForRow (e.Row, item.ID.Value);
			
			(e.Row.FindControl ("lnkUserEdit") as HyperLink).NavigateUrl = string.Format ("EditUser.aspx?ItemID={0}", item.ID);
			(e.Row.FindControl ("lnkUserEdit") as HyperLink).Text = item.calFullName;
			
			(e.Row.FindControl ("lnkUserEmail") as Label).Text = item.Email;
			
			(e.Row.FindControl ("lblUserIdentificationType") as Label).Text = item.IdentificationType.Code;
		}
	}
	
	#endregion
}
